package com.hudongwx.godoffood.fragment;


import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.activity.LoginActivity;
import com.hudongwx.godoffood.activity.myActivity.AboutUsActivity;
import com.hudongwx.godoffood.activity.myActivity.ChangePwdActivity;
import com.hudongwx.godoffood.activity.myActivity.CommunityMsgActivity;
import com.hudongwx.godoffood.activity.myActivity.MyCommunityActivity;
import com.hudongwx.godoffood.activity.myActivity.MyMore;
import com.hudongwx.godoffood.activity.myActivity.MyOrderActivity;
import com.hudongwx.godoffood.utils.MyApplication;

import org.w3c.dom.Text;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_my)
public class MyFragment extends Fragment {
    @ViewInject(R.id.my_head_name_tv)
    private TextView tvName;
    @ViewInject(R.id.my_head_icon_iv)
    private ImageView ivIcon;
    private String TAG = "MyFragment";
    @ViewInject(R.id.my_head_could_id)
    private TextView tvCould;
    @ViewInject(R.id.my_login_exit)
    TextView tvExitLogin;
//    @ViewInject(R.id.head_pop_bkg_iv)
//    ImageView ivBkg;

    private static String name, icon, couldId;
    private Intent intent;
    private PopupWindow iconPopwindow;
    private RelativeLayout iconRelative;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = x.view().inject(this, inflater, container);
        if (MyApplication.getStorage("Uid").equals("1")) {
            name = MyApplication.getStorage("name");
            icon = MyApplication.getStorage("id");
            couldId = MyApplication.getStorage("img");
            uiFlush();
        }
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //用于其他页面
        if (null != data) {
            MyApplication.storageIcon(data.getStringExtra("name"), data.getStringExtra("icon"), data.getStringExtra("couldId"));
            //设置头像那一块的属性
            name = MyApplication.getStorage("name");
            icon = MyApplication.getStorage("id");
            couldId = MyApplication.getStorage("img");
            uiFlush();
        }
    }

    //头像部分
    @Event(R.id.my_head_ll)
    private void toLogin(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            //弹出窗口
            popShow(view);
        } else {
            startActivityForResult(new Intent(getActivity(), LoginActivity.class), 2);

        }
    }

    //我的社区
    @Event(R.id.my_community_tv_1)
    private void toCommunity(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            uiFlush();
            intent = new Intent(getActivity(), MyCommunityActivity.class);
            intent.putExtra("name", name);
            intent.putExtra("icon", icon);
            intent.putExtra("couldId", couldId);
            startActivity(intent);
        } else {
            intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("UIid", 101);
            startActivityForResult(intent, 3);
        }

    }

    //我的账单
    @Event(R.id.my_order_tv_1)
    private void toOrder(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            uiFlush();
            startActivity(new Intent(getActivity(), MyOrderActivity.class));
        } else {
            intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("UIid", 102);
            startActivityForResult(intent, 4);
        }
    }

    //跳转更多
    @Event(R.id.my_more_rl)
    private void toMore(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            uiFlush();
            startActivity(new Intent(getActivity(), MyMore.class));
        } else {
            intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("UIid", 105);
            startActivityForResult(intent, 5);
        }

    }

    //更改密码
    @Event(R.id.my_change_pwd_rl)
    private void changePwd(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            uiFlush();
            startActivity(new Intent(getActivity(), ChangePwdActivity.class));
        } else {
            intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("UIid", 104);
            startActivityForResult(intent, 6);
        }
    }

    //我的信息
    @Event(R.id.my_msg_rl_1)
    private void msgCenter(View view) {
        if (MyApplication.getStorage("Uid").equals("1")) {
            uiFlush();
            startActivity(new Intent(getActivity(), CommunityMsgActivity.class));
        } else {
            intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("UIid", 103);
            startActivityForResult(intent, 7);
        }
    }

    //退出登陆
    @Event(R.id.my_login_exit)
    private void exitLogin(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View inView = LayoutInflater.from(getActivity()).inflate(R.layout.my_exit_login_dialog, null);
        builder.setView(inView);
        TextView tvCancel = (TextView) inView.findViewById(R.id.exit_login_tv_cancel);
        TextView tvYes = (TextView) inView.findViewById(R.id.exit_login_tv_yes);
        final AlertDialog dialog = builder.show();
        //弹窗取消
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //确定取消登陆状态  回恢复以前的状态并且关闭窗口
        tvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //改变登陆状态的存储状态
                MyApplication.storage("50");
                //返回登陆前的状态
                tvExitLogin.setVisibility(View.GONE);
                tvName.setText("请登录/注册");
                ivIcon.setImageResource(R.drawable.default_user_head_icon);
                tvCould.setText("");
                dialog.dismiss();
            }
        });
    }

    //更新头像区域
    private void uiFlush() {
        tvName.setText(name);
        x.image().bind(ivIcon, icon);
        tvCould.setText(couldId);
        tvExitLogin.setVisibility(View.VISIBLE);
    }

    /**
     * pop弹窗 实现更换头像
     *
     * @param view
     */
    private void popShow(View view) {
        if (null == iconPopwindow) {
            iconRelative = (RelativeLayout) LayoutInflater.from(getActivity()).inflate(R.layout.my_head_popup, null);
            iconPopwindow = new PopupWindow(iconRelative, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            iconPopwindow.showAtLocation(view, Gravity.TOP, 0, 0);
            final LinearLayout llContent = (LinearLayout) iconRelative.findViewById(R.id.head_pop_content_ll);
            final ImageView ivBkg = (ImageView) iconRelative.findViewById(R.id.head_pop_bkg_iv);
            //点击动画
            Animation animation = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f,
                    Animation.RELATIVE_TO_SELF, 1.0f,
                    Animation.RELATIVE_TO_SELF, 0.0f);
            animation.setDuration(400);
            animation.setInterpolator(new LinearInterpolator());
            transAnimOpen(ivBkg);
            llContent.setAnimation(animation);
            //点击取消
            TextView tvCancel = (TextView) llContent.findViewById(R.id.head_pop_cancel_tv);
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                closePopupWindows(ivBkg,llContent);
                }
            });
            //点击黑色的区域
            iconRelative.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    closePopupWindows(ivBkg,llContent);
                }
            });
            //跳转到相机
            TextView tvPhoto = (TextView) llContent.findViewById(R.id.head_pop_photo_tv);
            tvPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    // 指定开启系统相机的Action
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);

                    //从这
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    startActivity(intent);
                }
            });
            TextView tvPicture = (TextView) llContent.findViewById(R.id.head_pop_picture_tv);
            tvPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    // 指定开启系统相机的Action
                    intent.setAction(Intent.ACTION_PICK);
                    //从这
                    intent.setType("image/*");
                    startActivity(intent);
                }
            });
        }
    }
    /**
     * 关闭popupWindow
     * @param iv   背景图片
     * @param llContent 布局
     */
    private void closePopupWindows(final ImageView iv, final LinearLayout llContent) {
        //清除动画
        llContent.clearAnimation();
        Animation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f);
        animation.setDuration(400);
        animation.setInterpolator(new LinearInterpolator());
        llContent.setAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                transAnimClose(iv);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iconPopwindow.dismiss();
                iconPopwindow = null;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 背景出现动画
     *
     * @param iv
     */
    private void transAnimOpen(final ImageView iv) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.2f, 0.5f, 0.7f, 0.9f, 1);
        animator.setDuration(400);
        animator.setRepeatCount(0);
        animator.start();
        iv.setVisibility(View.VISIBLE);
    }

    /**
     * 背景消失动画
     *
     * @param iv
     */
    private void transAnimClose(final ImageView iv) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1, 0.9f, 0.7f, 0.5f, 0.2f, 0);
        animator.setDuration(400);
        animator.setRepeatCount(0);
        animator.start();
        animator.addListener(new Animator.AnimatorListener() {

            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                iv.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }
}
